package com.gxc.blogm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.util.CurrentUtil;
import com.gxc.blogm.domain.Frendlink;
import com.gxc.blogm.service.FrendlinkService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

public class FrendlinkController {

	FrendlinkService frendlinkService;

	public FrendlinkService getFrendlinkService() {
		return frendlinkService;
	}

	public void setFrendlinkService(FrendlinkService frendlinkService) {
		this.frendlinkService = frendlinkService;
	}

	/**
	 * 列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blogm/frendlink/frendlink_list.vm");
	}

	/**
	 * 列出一页公告
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = frendlinkService.listPage(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 新建公告页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("blogm/frendlink/frendlink_create.vm");
	}

	/**
	 * 新建公告保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		Frendlink frendlink = (Frendlink) RequestUtil.getInObject(request,
				Frendlink.class);
		frendlink.setId("frendlink_" + StringUtil.getUUID());
		frendlink.setCreate_user(CurrentUtil.getAuthUser().getId());
		frendlink.setCreate_time(new Date());
		int i = frendlinkService.create(frendlink);
		map.put("status", "success");
		map.put("data", i);
		String result = JsonUtil.object2json(map);
		response.getWriter().write(result);
		return null;
	}

	/**
	 * 修改公告页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Frendlink frendlink = frendlinkService.retrieve(id);
		Map model = new HashMap();
		model.put("frendlink", frendlink);
		return new ModelAndView("blogm/frendlink/frendlink_edit.vm", model);
	}

	/**
	 * 修改公告保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView editSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Frendlink frendlink = (Frendlink) RequestUtil.getInObject(request,
				Frendlink.class);
		int i = frendlinkService.update(frendlink);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除一个公告
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		int i = frendlinkService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除多个公告
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		String[] id_array = id.split(",");
		int i = frendlinkService.delete(id_array);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 查看公告
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Frendlink frendlink = frendlinkService.retrieve(id);
		Map model = new HashMap();
		model.put("frendlink", frendlink);
		return new ModelAndView("blogm/frendlink/frendlink_view.vm", model);
	}

}
